﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Zune2LyncNP.src
{
    public class Configuracion : INotifyPropertyChanged
    {
        private Configuracion() 
        {
            //FormatoCancion = "{0} - {1} ({2})";
            CargarConfiguracion();
        }

        
        private static Configuracion instancia = new Configuracion();
        public static Configuracion Instancia { get { return instancia; } }

        private string _formatoCancion;
        public string FormatoCancion
        {
            get
            {
                return _formatoCancion;
            }
            set
            {
                _formatoCancion = value;
                OnPropertyChanged("FormatoCancion");
            }
        }
        private bool _conservarEstadoAlCerrar;
        public bool ConservarEstadoALCerrar
        {
            get
            {
                return _conservarEstadoAlCerrar;
            }
            set
            {
                _conservarEstadoAlCerrar = value;
                OnPropertyChanged("ConservarEstadoALCerrar");
            }

        }


        //varas de eventos
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string info)
        {
            PersistirValores();
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }

        private void PersistirValores()
        {
            
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            //formatoCancion
            if (config.AppSettings.Settings["formatoCancion"] == null)
                config.AppSettings.Settings.Add("formatoCancion", FormatoCancion);
            else
                config.AppSettings.Settings["formatoCancion"].Value = FormatoCancion;
            //Conservar Estado
            if (config.AppSettings.Settings["ConservarEstado"] == null)
                config.AppSettings.Settings.Add("ConservarEstado", ConservarEstadoALCerrar.ToString());
            else
                config.AppSettings.Settings["ConservarEstado"].Value = ConservarEstadoALCerrar.ToString();
            
            config.Save(ConfigurationSaveMode.Modified);
        }
        private void CargarConfiguracion()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //FormatoCancion
            if (config.AppSettings.Settings["formatoCancion"] != null)
                _formatoCancion = config.AppSettings.Settings["formatoCancion"].Value;
            else
                FormatoCancion = "Now Playing: {0} - {1}";
            //ConservarEstado
            if (config.AppSettings.Settings["ConservarEstado"] != null)
                _conservarEstadoAlCerrar = bool.Parse(config.AppSettings.Settings["ConservarEstado"].Value);
            else
                ConservarEstadoALCerrar = true;
        }
    }
}
